package com.momo.mal.util.slidingmenu;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.momo.mal.About;
import com.momo.mal.Login;
import net.momo.mal.R;
import com.momo.mal.anime.overall.AnimeOverall;
import com.momo.mal.manga.overall.MangaOverall;
import com.momo.mal.util.DialogLogout;
import com.momo.mal.util.DialogSearchMangaAnime;
import com.momo.mal.util.Synchcronize;

public class ListMenu extends ListFragment {
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if(position == 2) {
			startActivity(new Intent(getActivity(), AnimeOverall.class));
		}
		if(position == 3) {
			startActivity(new Intent(getActivity(), MangaOverall.class));
		}
		if(position == 4) {
			new DialogSearchMangaAnime().newInstance().show(getActivity().getSupportFragmentManager(),
					 null);
		}
		if(position == 5) {
			new Synchcronize(getActivity()).execute();
		}
		if(position == 7) {
			new DialogLogout().newInstance(getActivity()).show(getActivity().getSupportFragmentManager(), null);
		}
		if(position == 8) {
			startActivity(new Intent(getActivity(), About.class));
		}
		if(position == 9) {
			try {
				startActivity(new Intent(Intent.ACTION_VIEW, Uri
						.parse("market://details?id=net.momo.mal")));
			} catch (android.content.ActivityNotFoundException anfe) {
				startActivity(new Intent(
						Intent.ACTION_VIEW,
						Uri.parse("http://play.google.com/store/apps/details?id=net.momo.mal")));
			}
		}
		if(position == 10) {
			try {
				String uri = "fb://page/162349703917418";
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				String uri = "https://www.facebook.com/momo.mal.community";
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
				startActivity(intent);
			}
		}
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		SampleAdapter adapter = new SampleAdapter(getActivity());
		
		String username = getActivity().getSharedPreferences(Login.PREFS_NAME,
				getActivity().MODE_PRIVATE).getString("username",
				"");
		adapter.add(new SampleItem("Welcome, " + username, R.drawable.ic_go));
		
		adapter.add(new SampleItem("Main Menu", R.drawable.ic_go));
		adapter.add(new SampleItem("My Anime List", R.drawable.ic_go));
		adapter.add(new SampleItem("My Manga List", R.drawable.ic_go));
		adapter.add(new SampleItem("Search", R.drawable.ic_go));
		adapter.add(new SampleItem("Synchronize", R.drawable.ic_go));
		
		adapter.add(new SampleItem("Preference", R.drawable.ic_go));
		adapter.add(new SampleItem("Logout", R.drawable.ic_go));
		adapter.add(new SampleItem("About", R.drawable.ic_go));
		adapter.add(new SampleItem("Rate App", R.drawable.ic_go));
		adapter.add(new SampleItem("Like us on Facebook", R.drawable.ic_go));
		
		setListAdapter(adapter);
	}

	private class SampleItem {
		public String tag;
		public int iconRes;
		public SampleItem(String tag, int iconRes) {
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				if(position == 0) {
					convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_username, null);
					TextView title = (TextView) convertView.findViewById(R.id.row_username);
					title.setText(getItem(position).tag);
				} else if(position == 1 || position == 6) {
					convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_title, null);
					TextView title = (TextView) convertView.findViewById(R.id.row_title);
					title.setText(getItem(position).tag);
				} else {
					convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
					ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
					icon.setImageResource(getItem(position).iconRes);
					TextView title = (TextView) convertView.findViewById(R.id.row_title);
					title.setText(getItem(position).tag);
				}
			}
			

			return convertView;
		}

	}
}
